<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use View;

class AdminBaseController extends Controller
{
    protected $model_obj;
    protected $model_keyname;
    protected $module;
    protected $option;

    public function setModelObj($model,$option)
    {
        $this->model_obj = $model;
        $this->model_keyname = $model->getKeyName();
        $this->module = $option['module_name'];
        $this->option = $option;
    }

    /**
     * Bootstrap-table 列表展示页面
     */
    public function listData(Request $request,$id=null)
    {
        $searchdata = $request->all();
        $view_url = "admin.{$this->module}.{$this->module}list";
        return View::make($view_url,['searchdata'=>$searchdata]);
    }

    /**
     * 异步数据列表
     * Bootstrap-table 异步获取数据
     */
    public function bootstrapTableList(Request $request)
    {
        $data = $request->all();
        $skip = $data['pagenum'];
        $take = $data['limitnum'];
        $search = isset($data['search']) ? $this->getTrueSearch($data['search']) : [];
        $orderby = $this->getTrueOrder($data);
        $feedback_list = (object)[];
        $feedback_list->data = $this->model_obj->orderBy($orderby['field'],$orderby['type'])->where($search)->skip($skip)->take($take)->get();
        $feedback_list->total = $this->model_obj->where($search)->count();
        $feedback_list_json = json_encode($feedback_list);
        return $feedback_list_json;
    }

    /**
     * Bootstrap-table 异步数据---排序
     * 由子类进行重写
     */
    protected function getTrueOrder($data)
    {
        $orderby['field'] = $this->model_keyname;
        $orderby['type'] = 'desc';
        return $orderby;
    }

    /**
     * Bootstrap-table 异步数据---搜索
     * 由子类进行重写
     */
    protected function getTrueSearch($serachstr)
    {
        return [];
    }

    /**
     * 数据添加页面
     */
    protected function addDataView()
    {
        $view_url = "admin.{$this->module}.{$this->module}add";
        return View::make($view_url);
    }

    /**
     * 数据更新页面
     */
    public function updateDataView($id)
    {
        $datainfo = $this->model_obj->find($id);
        $view_url = "admin.{$this->module}.{$this->module}edit";
        return View::make($view_url, ['datainfo'=>$datainfo]);
    }
	
    /**
     * 数据详情页面
     */
    /*public function detailsDataView($id)
    {
        $datainfo = $this->model_obj->find($id);
        $view_url = "admin.{$this->module}.details";
        return View::make($view_url, ['datainfo'=>$datainfo]);
    }*/

    /**
     * 数据详情页面
     */
    public function detailsDataView($id)
    {
        $datainfo = $this->model_obj->find($id);
        $view_url = "admin.{$this->module}.{$this->module}details";
        return View::make($view_url, ['datainfo'=>$datainfo]);
    }

    /**
     * 数据删除
     * 包括单点删除和多项删除
     */
    protected function toDelData(Request $request)
    {
        $ids = explode(',', $request->get('id'));
        $enable_del = $this->enableToDel($ids);
        if($enable_del){
            $re = $this->model_obj->destroy($ids);
            $jsonData = $re ? $this->getJsonData(3,'删除成功') : $this->getJsonData(0,'删除失败');
        }else{
            $jsonData = $this->getJsonData(0,'删除项中有关联数据，请先删除关联项');
        }
        return response()->json($jsonData);
    }

    /**
     * 判断数据是否可删除
     * 父类默认返回 True
     */
    protected function enableToDel($ids)
    {
        return true;
    }

    /**
     * 数据状态变更
     * 状态字段统一为 status
     */
    public function toStatusData($id, $status=0)
    {
        $info = $this->model_obj->find($id);
        $filed_status = $this->option['status'];
        $info->$filed_status = $status;
        $re = $info->save();
        $jsonData = $re ? $this->getJsonData(3,'变更成功') : $this->getJsonData(0,'变更失败');
        return response()->json($jsonData);
    }

    /**
     * 数据备注（后台操作）
     * 状态字段统一为 adminremark
     */
    protected function toRemarkData(Request $request)
    {
        $id = $request->get('id');
        $re = $this->model_obj->where($this->model_keyname,$id)->update(['adminremark'=>$request->get('content')]);
        $jsonData = $re ? $this->getJsonData(3,'备注成功') : $this->getJsonData(0,'备注失败');
        return response()->json($jsonData);
    }

}